package com.sl.water.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.sl.water.entity.BThresholdWater;
import com.sl.water.service.BThresholdWaterService;
import com.sl.water.service.ForecastSchemeService;
import com.sl.water.service.WaterWarnService;
import com.sl.water.vo.ReservoirWarnVO;
import com.sl.water.vo.RiverWarn;
import com.sl.water.vo.RiverWarnVO;
import com.sl.water.vo.RsvrWarn;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: duan
 * @time: 2025-05-26 15:05
 */
@Service
@RequiredArgsConstructor
public class WaterWarnServiceImpl implements WaterWarnService {

    private final JdbcTemplate jdbcTemplate;
    private final ForecastSchemeService forecastSchemeService;
    private final BThresholdWaterService bThresholdWaterService;
    private static final String[] STATIONS = {"吴家闸", "刘家闸"};

    @Override
    public ReservoirWarnVO getReservoirWarnVO() {
        LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
        String nowDate = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_FORMATTER);
        //查询水库水情预警情
        String sql = "SELECT * FROM st_r_rsvr WHERE stcd=(SELECT station_code FROM st_station_b WHERE station_name='赵庄水库水位站') " +
                "AND tm<='" + nowDate + "' ORDER BY tm desc LIMIT 1";
        ReservoirWarnVO reservoirWarnVO = new ReservoirWarnVO();
        Double otq = 0.0, z = 0.0;
        List<Map<String, Object>> maps = this.jdbcTemplate.queryForList(sql);
        if (!maps.isEmpty()) {
            Map<String, Object> reservoirMap = maps.get(0);
            z = Double.parseDouble(reservoirMap.get("rz").toString());
            otq = Double.parseDouble(reservoirMap.get("otq").toString());
            reservoirWarnVO.setInq(Double.parseDouble(reservoirMap.get("inq").toString()));
            reservoirWarnVO.setZ(z);
            reservoirWarnVO.setOtq(otq);
        }
        //去forecastScheme表中查询预报水位
        Double zYb = forecastSchemeService.getForecastSchemeDTO().getReservoirMaxWaterLevelOrDefault();
        reservoirWarnVO.setZYb(zYb);
        //根据水库水位查询预警级别
        BThresholdWater zBThresholdWater = bThresholdWaterService.getByKindAndThreshold("水库水位", z);
        //根据预报水位查询预警级别
        BThresholdWater ybBThresholdWater = bThresholdWaterService.getByKindAndThreshold("水库水位", zYb);
        //根据出库流量查询预警级别
        BThresholdWater otpBThresholdWater = bThresholdWaterService.getByKindAndThreshold("水库出库流量", otq);
        reservoirWarnVO.setZLevel(zBThresholdWater.getLevel());
        reservoirWarnVO.setZLevelName(zBThresholdWater.getName());
        reservoirWarnVO.setZYbLevel(ybBThresholdWater.getLevel());
        reservoirWarnVO.setZYbLevelName(ybBThresholdWater.getName());
        reservoirWarnVO.setOtqLevel(otpBThresholdWater.getLevel());
        reservoirWarnVO.setOtqLevelName(otpBThresholdWater.getName());
        return reservoirWarnVO;
    }

    @Override
    public List<RiverWarnVO> getRiverWarnVOList() {
        List<RiverWarnVO> riverWarnVOList = new ArrayList<>();
        LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
        String nowDate = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_FORMATTER);
        for (String station : STATIONS) {
            RiverWarnVO riverWarnVO = new RiverWarnVO();
            String name = "";
            Double z = 0.0, q = 0.0;
            String sql = "SELECT st_station_b.river_name,st_r_river.* FROM st_r_river LEFT JOIN st_station_b" +
                    " ON st_r_river.stcd=st_station_b.station_code" +
                    " WHERE st_station_b.station_name='" + station + "' AND tm<='" + nowDate + "' ORDER BY tm desc LIMIT 1";
            List<Map<String, Object>> rivers = this.jdbcTemplate.queryForList(sql);
            if (!rivers.isEmpty()) {
                Map<String, Object> riverMap = rivers.get(0);
                name = String.valueOf(riverMap.get("river_name"));
                z = Double.parseDouble(riverMap.get("z").toString());
                q = Double.parseDouble(riverMap.get("q").toString());
            }
            //去forecastScheme表中查询河道预报流量
            Double qYb = forecastSchemeService.getForecastSchemeDTO().getRiverSectionAnalysisDTOMaxFlowOrDefault();
            //根据水位查询预警级别
            BThresholdWater zBThresholdWater = bThresholdWaterService.getByKindAndThreshold("河道水位", z);
            //根据预报水位查询预警级别
            BThresholdWater qYbBThresholdWater = bThresholdWaterService.getByKindAndThreshold("河道流量", qYb);
            //根据河道流量查询预警级别
            BThresholdWater qBThresholdWater = bThresholdWaterService.getByKindAndThreshold("河道流量", q);
            riverWarnVO.setName(name);
            riverWarnVO.setZ(z);
            riverWarnVO.setQ(q);
            riverWarnVO.setQYb(qYb);
            riverWarnVO.setZLevel(zBThresholdWater.getLevel());
            riverWarnVO.setZLevelName(zBThresholdWater.getName());
            riverWarnVO.setQLevel(qBThresholdWater.getLevel());
            riverWarnVO.setQLevelName(qBThresholdWater.getName());
            riverWarnVO.setQYbLevel(qYbBThresholdWater.getLevel());
            riverWarnVO.setQYbLevelName(qYbBThresholdWater.getName());
            riverWarnVOList.add(riverWarnVO);
        }
        return riverWarnVOList;
    }

    @Override
    public RsvrWarn getRsvrWarnAfterTime(Long id, LocalDateTime time) {
        //查询水库水情预警情
        RsvrWarn rsvrWarn = new RsvrWarn();
        String startTime = LocalDateTimeUtil.format(time, DatePattern.NORM_DATETIME_FORMATTER);
        String endTime = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_FORMATTER);
        String sql = "SELECT id,tm,rz,otq FROM st_r_rsvr WHERE id>" + id + " and tm between '" + startTime + "' and  '" + endTime + "' ORDER BY id asc LIMIT 1";
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
        if (list.isEmpty()) {
            return null;
        }
        Map<String, Object> rsvrMap = list.get(0);
        Double z = Double.parseDouble(String.valueOf(rsvrMap.get("rz")));
        Double otq = Double.parseDouble(String.valueOf(rsvrMap.get("otq")));
        rsvrWarn.setId(Long.parseLong(String.valueOf(rsvrMap.get("id"))));
        rsvrWarn.setTm(LocalDateTime.parse(String.valueOf(rsvrMap.get("tm"))));
        rsvrWarn.setZ(z);
        rsvrWarn.setOtq(otq);
        //根据水库水位查询预警级别
        BThresholdWater zBThresholdWater = bThresholdWaterService.getByKindAndThreshold("水库水位", z);
        //根据出库流量查询预警级别
        BThresholdWater otpBThresholdWater = bThresholdWaterService.getByKindAndThreshold("水库出库流量", otq);
        rsvrWarn.setZLevel(zBThresholdWater.getLevel());
        rsvrWarn.setZLevelName(zBThresholdWater.getName());
        rsvrWarn.setZThreshold(zBThresholdWater.getThreshold());
        rsvrWarn.setOtqLevel(otpBThresholdWater.getLevel());
        rsvrWarn.setOtqLevelName(otpBThresholdWater.getName());
        rsvrWarn.setOtqThreshold(otpBThresholdWater.getThreshold());
        return rsvrWarn;
    }

    @Override
    public RiverWarn getRiverWarnAfterTime(Long id, LocalDateTime time) {
        RiverWarn riverWarn = new RiverWarn();
        String startTime = LocalDateTimeUtil.format(time, DatePattern.NORM_DATETIME_FORMATTER);
        String endTime = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_FORMATTER);
        String sql = "SELECT id,tm,z,q,(SELECT station_name FROM st_station_b s WHERE  s.station_code=r.stcd) as n FROM st_r_river r where id>" + id + " and tm between '" + startTime + "' and  '" + endTime + "' ORDER BY id asc LIMIT 1";
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
        if (list.isEmpty()) {
            return null;
        }
        Map<String, Object> riverMap = list.get(0);
        riverWarn.setId(Long.parseLong(String.valueOf(riverMap.get("id"))));
        riverWarn.setName(riverMap.get("n").toString());
        riverWarn.setTm(LocalDateTime.parse(String.valueOf(riverMap.get("tm"))));
        Double z = Double.parseDouble(riverMap.get("z").toString());
        Double q = Double.parseDouble(riverMap.get("q").toString());
        riverWarn.setZ(z);
        riverWarn.setQ(q);
        //根据水位查询预警级别
        BThresholdWater zBThresholdWater = bThresholdWaterService.getByKindAndThreshold("河道水位", z);
        //根据河道流量查询预警级别
        BThresholdWater qBThresholdWater = bThresholdWaterService.getByKindAndThreshold("河道流量", q);
        riverWarn.setZLevel(zBThresholdWater.getLevel());
        riverWarn.setZLevelName(zBThresholdWater.getName());
        riverWarn.setZThreshold(zBThresholdWater.getThreshold());
        riverWarn.setQLevel(qBThresholdWater.getLevel());
        riverWarn.setQLevelName(qBThresholdWater.getName());
        riverWarn.setQThreshold(qBThresholdWater.getThreshold());
        return riverWarn;
    }
}
